package com.rz.paas.test.SettingDetail

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class StickHeaderDirection(val context: Context, val enable: Boolean = true) :
    RecyclerView.ItemDecoration() {


    val itemHeaderHeight by lazy {
        dp2px(40)
    }

    val textPaddingLeft by lazy {
        dp2px(6)
    }

    val textRect by lazy {
        Rect()
    }

    val itemHeaderPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }

    val textPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }

    private fun dp2px(dp: Int): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dp * 1.0f,
            context.resources.displayMetrics
        )
    }

    init {
        itemHeaderPaint.color = Color.parseColor("#f7f8fa")
        textPaint.textSize = 46f
        textPaint.color = Color.parseColor("#cfd3de")
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.adapter is SettingDetailAdapter) {
            var adapter = parent.adapter as SettingDetailAdapter
            var childCount = parent.childCount
            for (index in 0 until childCount) {
                var view = parent.getChildAt(index)
                var position = parent.getChildLayoutPosition(view)
                var isHeader = adapter.isItemHeader(position)
                var left = parent.paddingLeft
                var right = parent.width - parent.paddingRight

                if (isHeader) {
                    c.drawRect(
                        left * 1.0f,
                        view.top - itemHeaderHeight,
                        right * 1.0f,
                        view.top * 1.0f,
                        itemHeaderPaint
                    )
                    var groupName = adapter.data[position].groupName
//                    textPaint.getTextBounds(groupName, 0, groupName.length, textRect)
//                    var dy = (textRect.top - textRect.bottom) / 2 + textRect.bottom
//                    c.drawText(  groupName,     left + textPaddingLeft,
//                        view.top / 2 * 1.0f + dy,
//                        textPaint
//                    )

                    textPaint.getTextBounds(
                        groupName,
                        0,
                        groupName.length,
                        textRect
                    )
                    c.drawText(
                        groupName,
                        left + textPaddingLeft.toFloat(),
                        view.top - itemHeaderHeight + itemHeaderHeight / 2 + (textRect.height() / 2).toFloat(),
                        textPaint
                    )

                }

            }
        }

    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        if (parent.adapter is SettingDetailAdapter && enable) {
            var adapter = parent.adapter as SettingDetailAdapter
            var position =
                ((parent.layoutManager) as LinearLayoutManager).findFirstVisibleItemPosition()
            var view = parent.findViewHolderForAdapterPosition(position)?.itemView!!
            var isHeader = adapter.isItemHeader(position + 1)
            var top = parent.paddingTop
            var left = parent.paddingLeft
            var right = parent.width - parent.paddingRight
            var groupName = adapter.data.get(position).groupName
            textPaint.getTextBounds(groupName, 0, groupName.length, textRect)
            var dy = (textRect.top - textRect.bottom) / 2 + textRect.bottom
            if (isHeader) {
                var bottom = itemHeaderHeight.coerceAtMost(view.bottom * 1.0f)

                c.drawRect(
                    left.toFloat(),
                    top + view.top - itemHeaderHeight * 1.0f,
                    right.toFloat(),
                    top + bottom,
                    itemHeaderPaint
                )
                textPaint.getTextBounds(
                    groupName,
                    0,
                    groupName.length,
                    textRect
                )
                c.drawText(
                    groupName,
                    left + textPaddingLeft,
                    top + itemHeaderHeight / 2 + textRect.height() / 2 - (itemHeaderHeight - bottom).toFloat(),
                    textPaint
                )

            } else {


                c.drawRect(
                    left.toFloat(),
                    top.toFloat(),
                    right.toFloat(),
                    top + itemHeaderHeight.toFloat(),
                    itemHeaderPaint
                )
                textPaint.getTextBounds(
                    groupName,
                    0,
                    groupName.length,
                    textRect
                )
                c.drawText(
                    groupName,
                    left + textPaddingLeft.toFloat(),
                    top + itemHeaderHeight / 2 + (textRect.height() / 2).toFloat(),
                    textPaint
                )

            }
            c.save()
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.adapter is SettingDetailAdapter) {
            var adapter = parent.adapter as SettingDetailAdapter
            var position = parent.getChildLayoutPosition(view)
            var itemHeader = adapter.isItemHeader(position)
            if (itemHeader) {
                outRect.top = itemHeaderHeight.toInt()
            }
        }
    }

}